package com.student.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.student.bean.HealthNews;
import com.student.bean.User;
import com.student.mapper.AdminMapper;
import com.student.service.AdminService;
import com.student.utils.AjaxResult;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    @Override
    public User selectUserByLoginName(@Param(value = "loginName") String loginName) {
        return adminMapper.selectByLogName(loginName);
    }

    @Override
    public void releaseNews(HealthNews news) {
        adminMapper.insert(news);
        Integer newsId = news.getNewid();
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Integer userId =  user.getUserid();

        adminMapper.insertNewsAuthor(newsId,userId);

    }

    @Override
    public AjaxResult allnews() {
        List<HealthNews> newsList = adminMapper.allNews();
        return AjaxResult.getOk(newsList);
    }

    @Override
    public AjaxResult allUser(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null?1:pageNum;
        pageSize = pageSize == null?5:pageSize;
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList = adminMapper.allUser();
        PageInfo info = new PageInfo(userList);
        return AjaxResult.getOK(info);
    }


}
